package com.zhenhao.xsq.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.zhenhao.xsq.R;

public class MySeekBar extends SeekBar {

    private PopupWindow mPopupWindow;

    private View mView;

    private TextView dialogSeekTime;

    private int[] mPosition;

    private final int mThumbWidth = 25;

    public MySeekBar(Context context) {
        super(context, null);
    }

    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mView = LayoutInflater.from(context).inflate(R.layout.seek_popu, null);
        dialogSeekTime = mView.findViewById(R.id.tv_seek_time);
        mPopupWindow = new PopupWindow(mView, mView.getWidth(), mView.getHeight());
        mPosition = new int[2];
    }

    private int getViewWidth(View v) {
        int w = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        return v.getMeasuredWidth();
    }

    private int getViewHeight(View v) {
        int w = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        return v.getMeasuredHeight();
    }

    public void hideSeekDialog() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    public void showSeekDialog(String str) {
        dialogSeekTime.setText(str);
        int progress = this.getProgress();
        int thumb_x = (int) (progress * (1.0f * (this.getWidth() - 22) / this.getMax()));
        int middle = this.getHeight() / 2 + 120;
        if (mPopupWindow != null) {
            try {
                this.getLocationOnScreen(mPosition);
                mPopupWindow.showAsDropDown(this, mPosition[0], mPosition[1]);
                int x = thumb_x + mPosition[0] - getViewWidth(mView) / 2 + mThumbWidth / 2;
                mPopupWindow.update(x, middle, getViewWidth(mView), getViewHeight(mView));
            } catch (Exception e) {

            }
        }
    }

}
